package cqrtplm.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.ec.module.vo.ChgFormVO;
import com.hustcad.plm.pdm.enumeration.service.TyplmEnumerationService;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.entity.RtChangeaffectChangeobjectDO;
import cqrtplm.mapper.RtChangeaffectChangeobjectMapper;
import cqrtplm.service.RTChgFormDataVerifyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RTChgFormDataVerifyServiceImpl implements RTChgFormDataVerifyService {
    private static final int MAX_RETRY = 150; //设置最大重试次数防止死循环
    @Resource
    private RtChangeaffectChangeobjectMapper rtChangeaffectChangeobjectMapper;
    @Resource
    private TyplmEnumerationService typlmEnumerationService;
    @Resource
    private TyplmWorkFlowContainerService typlmWorkFlowContainerService;

    /**
     * 校验变更表单
     *
     * @param workFlowContainerId
     * @return
     */
    @Override
    public void checkFormData(String workFlowContainerId, String nodeName) {
        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(new BigInteger(workFlowContainerId));
        if (!Objects.isNull(workFlowContainerView) && workFlowContainerView instanceof FlowChgFormView) {
            FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
            //获取变更通告对象
            ChgFormVO chgFormVO = flowChgFormView.getChgFormVO();
            String ecnNumber = chgFormVO.getObjectNumber();
            List<String> errorList = new ArrayList<>();
            if (ecnNumber.contains("RTTJST")) {
                String formData = chgFormVO.getFormData();
                StringBuffer sb = new StringBuffer();
                Map<String, String> enmumap = this.typlmEnumerationService.getEnabledEnumItemsMap(Collections.singletonList("relatedModels")).get("relatedModels");
                List<RtChangeaffectChangeobjectDO> affectList = rtChangeaffectChangeobjectMapper.queryByChangeNoAndPartNo(ecnNumber, "");
                for (RtChangeaffectChangeobjectDO changeaffectChangeobjectDO : affectList) {
                    String productType = changeaffectChangeobjectDO.getDevcode();
                    if (StringUtils.isNotBlank(productType)) {
                        if (StringUtils.isNotBlank(sb.toString())) {
                            sb.append(",");
                        }
                        sb.append(productType);
                    }
                }
                JSONObject formDataJson = JSONObject.parseObject(formData);
                if ("会签(关联变更确认)".equals(nodeName)) {
                    String GLBGPD = formDataJson.getString("GLBGPD");
                    if (StringUtils.isNotBlank(GLBGPD) && (GLBGPD.equals("1") || GLBGPD.equals("否"))) {
                        errorList.add("关联变更判定为“否”，该零件更改后不能满足其他被使用机型或客户，不能更改，请新增状态！");
                    }
                }
                if ("启动流程".equals(nodeName)) {
                    if (StringUtils.isNotBlank(sb.toString())) {
                        List<String> productTypeList = Arrays.asList(sb.toString().split(","));
                        productTypeList = productTypeList.stream().distinct().filter(productType -> enmumap.containsKey(productType)).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(productTypeList)) {
                            JSONArray subform63964 = formDataJson.getJSONArray("subform63964");
                            if (CollectionUtils.isEmpty(subform63964)) {
                                errorList.add("请在“关联机型”中点击收集按钮收集机型工程师!");
                            }
                        }
                    }
                    String SFGGZTDD = formDataJson.getString("SFGGZTDD");
                    if (StringUtils.isNotBlank(SFGGZTDD) && ("是".equals(SFGGZTDD))) {
                        JSONArray subform94192 = formDataJson.getJSONArray("subform94192");
                        if (CollectionUtils.isEmpty(subform94192)) {
                            errorList.add("请维护表单中的订单更改数据！");
                        }
                    }
                }
                if ("协同(工艺工程部）".equals(nodeName)) {
                    String SFGGGYWJ = formDataJson.getString("SFGGGYWJ");
                    if (StringUtils.isNotBlank(SFGGGYWJ) && ("是".equals(SFGGGYWJ) || "1".equals(SFGGGYWJ))) {
                        String GYBGDBH = formDataJson.getString("GYBGDBH");
                        if (StringUtils.isBlank(GYBGDBH)) {
                            errorList.add("请填写“工艺变更单编号”!");
                        }
                    }
                }
                if ("协同(重庆SQE签名）".equals(nodeName)) {
                    String CQSFGJJ = formDataJson.getString("CQSFGJJ");
                    if (StringUtils.isNotBlank(CQSFGJJ) && ("是".equals(CQSFGJJ) || "1".equals(CQSFGJJ))) {
                        String CQJJBH = formDataJson.getString("CQJJBH");
                        if (StringUtils.isBlank(CQJJBH)) {
                            errorList.add("请填写“检基编号”!");
                        }
                    }
                }
                if ("协同(越南SQE签名）".equals(nodeName)) {
                    String YNSFGJJ = formDataJson.getString("YNSFGJJ");
                    if (StringUtils.isNotBlank(YNSFGJJ) && ("是".equals(YNSFGJJ) || "1".equals(YNSFGJJ))) {
                        String YNJJBH = formDataJson.getString("YNJJBH");
                        if (StringUtils.isBlank(YNJJBH)) {
                            errorList.add("请填写“检基编号”!");
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(errorList))
                throw RTErrorCodeEnum.CUSTOM_EXCEPTION.getException(String.join("，", errorList));
        }
    }
}
